package com.tianji.StragyHttp.utils;


import com.tianji.exception.BizException;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @ClassName SessionUtil
 * @Description
 * @Author yuwei
 * @Date 2020/6/17 14:40
 * @Version 1.0
 */
@Slf4j
public class SessionUtil {

    /**
     * session 失效时间
     */
    private static final Integer SESSION_OUT_TIME = 8;

    public static String SESSION_USER_INFO_KEY = "yunque:sId:";


    /**
     * 获取当前登陆用户信息
     *
     * @return
     */
    public static UserSession getUser() {
        HttpServletRequest request = HttpKit.getRequest();
        HttpSession session = request.getSession();
        String key = SESSION_USER_INFO_KEY + session.getId();
        UserSession userSession = RedisKit.get(key, UserSession.class);
        if (userSession == null) {
            throw new BizException("未登陆");
        }
        return userSession;
    }

    /**
     * 判断是否登录
     * @return
     */
    public static Boolean isLogin(){
        HttpServletRequest request = HttpKit.getRequest();
        HttpSession session = request.getSession();
        String key = SESSION_USER_INFO_KEY + session.getId();
        UserSession userSession = RedisKit.get(key,UserSession.class);
        if(userSession == null){
            return false;
        }
        return true;
    }

    /**
     * 设置用户缓存
     *
     * @param userSession
     */
    public static void setSession(UserSession userSession) {
        HttpServletRequest request = HttpKit.getRequest();
        String sessionId = request.getSession().getId();
        String key = SESSION_USER_INFO_KEY + sessionId;
        //判断当前session是否登录其他客户账号，如果有则先登出前一账号
        if (RedisKit.hasKey(key)) {
            //移除
            RedisKit.del(key);
        }
        //再存入新用户信息
        RedisKit.set(key, userSession, SESSION_OUT_TIME, TimeUnit.HOURS);
        log.info("用户【{}】缓存设置成功,sessionId={}", userSession.getUserId(),sessionId);
    }

    /**
     * 清除当前登陆用户缓存
     */
    public static void removeSession(){
        HttpServletRequest request = HttpKit.getRequest();
        String sessionId = request.getSession().getId();
        String key = SESSION_USER_INFO_KEY + sessionId;
        UserSession userSession = RedisKit.get(key,UserSession.class);
        if(userSession == null){
            return;
        }
        RedisKit.del(key);
        log.info("用户【{}】缓存移除成功,sessionId={}",userSession.getUserId(),sessionId);
    }


    /**
     * 更新用户缓存（用于用户信息变更时，更新相关缓存）
     * @param sellerNick
     */
    public static void updateSession(String sellerNick,UserSession userSession){
        String sessionId = HttpKit.getRequest().getSession().getId();
        String key = SESSION_USER_INFO_KEY + sessionId;
        long expire = RedisKit.getExpire(key,TimeUnit.SECONDS);
        if(expire > 0){
            RedisKit.set(key,userSession,expire,TimeUnit.SECONDS);
        }
        log.info("用户【{}】缓存更新成功,sessionId={}",sellerNick,sessionId);
    }

}
